package org.wiztools.awsabstraction;

/**
 *
 * @author subwiz
 */
public class AWSTimeoutThreadLocal<T> extends ThreadLocal<T> {
    
    private long lastCreatedValue = -1;
    
    @Override
    public T get() {
        if(lastCreatedValue == -1) {
            lastCreatedValue = System.currentTimeMillis();
        }
        // A client can be used only for 300 seconds (3,00,000 milliseconds).
        // So a new client needs to be created if the last created client
        // session is over 250 seconds old:
        if((System.currentTimeMillis() - lastCreatedValue) > 250000) {
            this.remove();
            lastCreatedValue = System.currentTimeMillis();
        }
        T obj = super.get();
        return obj;
    }
}
